<?php

/**
 * 基础库管理
 *各类下拉框设置 、暂且是这样
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/10/27
 * Time: 10:02
 */

namespace app\api\controller\basemanage;
use app\api\controller\common\ApiBase;
use think\Controller; // den
use think\Db;
class Subscribeserver extends ApiBase
{

    /**
     *
     * @return \think\response\Json|void
     *  comppub
     *
     */

    public function getList(){
        $page = $this->request->param('pageIndex');
        $keyword = $this->request->param('title');
        $cate = $this->request->param('cate');
        $page = max(1,intval($page));
        $pageSize =$this->request->param('pageSize')==""?20:$this->request->param('pageSize');
        $param=" and s.is_delete=0";
        if (!empty($keyword)) {
            $param.=" and s.name like '%$keyword%' ";
        }
        if(!empty($cate)){
            $cRes=db('server_cate')->where(['is_delete'=>0,'id'=>$cate])->find();
            if($cRes){
                if($cRes['parent_id'] == 0){
                    $param.=" and s.cate_id in (select id form crm_server_cate where parent_id=$cate) ";
                }else{
                    $param.=" and s.cate_id=$cate ";
                }
            }else{
                $param.=" and s.cate_id=$cate ";
            }

        }
        $sql2="select s.*,c.title as cate_name from crm_server_subscribe as s ";
        $sql2.="left join crm_server_cate_subscribe as c on c.id=s.cate_id";
        $sql2.=" where 1=1 $param";
        $sql2.=" order by s.sort desc";
        $total1=Db::query($sql2);
        $sql2.=" limit ".($page-1)*$pageSize.",$pageSize";
        $list=Db::query($sql2);
        $total=count($total1); //st
        return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'total'=>$total]]);
    }


    /**
     * 添加
     * @return \think\response\Json|void
     *  zs_question       zs_question_option
     *  zs_question_type
     */
    public function add(){
        $data['create_time']=time();
        $data['update_time']=time();
        $data['name']=$this->request->param('name');   // 1单选 2多选3  填空
        $data['sort']=$this->request->param('sort');   // 1单选 2多选3  填空
        $data['status']=$this->request->param('status');
        $data['cover']=$this->request->param('cover');
        $data['cate_id']=$this->request->param('cate_id');
        $data['price']=$this->request->param('price');
        $data['cate_parent_ids']=$this->request->param('cate_parent_ids');
        $data['skill']=$this->request->param('skill');
        $data['description']=$this->request->param('description');
        $data['content']=$this->request->param('content');
        $res=db('server_subscribe')->insert($data);
        if(!$res){
            return json(['code'=>0,'msg'=>'操作失败','data'=>'']);
        }
        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
    }

    /**
     * 删除
     */
    public function del(){
        $id = $this->request->param('ids');
        $ids = explode(',',$id);
        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误','data'=>'']);
        }
        $msg='';
        $flag=true;
        foreach ($ids as $id){
            $r = db('server_subscribe')->where(['id'=>$id])->find();
            if(!$r){
                $flag=false;
                $msg='id:'.$id.'记录不存在';
                break;
            }
        }

        if(!$flag){
            return json(['code'=>0,'msg'=>$msg]);
        }

        foreach ($ids as $id){
            $r = db('server_subscribe')->where(['id'=>$id])->update(['is_delete'=>1]);
            if(!$r){
                $flag=false;
                $msg='id:'.$id.'删除失败请重试';
                break;
            }
        }

        if(!$flag){
            return json(['code'=>0,'msg'=>$msg]);
        }
        return json(['code'=>1,'msg'=>'操作成功']);

    }


    /**
     * 基本信息修改
     * @return \think\response\Json|void
     */
    public function edit()
    {
//        die();
        $id = $this->request->param('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }
        $data['update_time']=time();
        $data['name']=$this->request->param('name');   // 1单选 2多选3  填空
        $data['sort']=$this->request->param('sort');   // 1单选 2多选3  填空
        $data['status']=$this->request->param('status');
        $data['cover']=$this->request->param('cover');
        $data['cate_id']=$this->request->param('cate_id');
        $data['cate_parent_ids']=$this->request->param('cate_parent_ids');
        $data['skill']=$this->request->param('skill');
        $data['price']=$this->request->param('price');
        $data['description']=$this->request->param('description');
        $data['content']=$this->request->param('content');
        $where['id'] = $id;
        $r=db('server_subscribe')->where($where)->update($data);
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }
        return json(['code' => 1, 'msg' => '操作成功']);
    }

    /**
     * 基本信息修改
     * @return \think\response\Json|void
     */
    public function getDetail()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }
        $where['id'] = $id;
        $r=db('server_subscribe')->where($where)->find();
        return json(['code' => 1, 'msg' => '操作成功','data'=>$r]);
    }

    /**
     * 基本信息修改
     * @return \think\response\Json|void
     */
    public function serveCateList()
    {
        $parent=db('server_cate_subscribe')->where(['is_delete'=>0])->select();
//        $parent=db('server_cate_subscribe')->where(['is_delete'=>0,'parent_id'=>0])->select();
//        $list=db('server_cate_subscribe')->where(['is_delete'=>0,'parent_id'=>array('gt',0)])->select();
//        foreach ($parent as &$v){
//            $v['children']=[];
//            foreach ($list as &$vv){
//                if($vv['parent_id']==$v['id']){
//                    $v['children'][]=$vv;
//                }
//            }
//        }
        return json(['code'=>1,'msg'=>'success','data'=>$parent]);
    }


}
